package com.cubicleman.airvj.delegates
{
	import com.amazon.ResultItem;
	import com.cubicleman.airvj.model.vo.LyricsRequest;
	import com.cubicleman.airvj.model.vo.MTV.Artist;
	import com.cubicleman.airvj.model.vo.MTV.ThumbNail;
	import com.cubicleman.airvj.model.vo.MTV.VideoVO;
	import com.cubicleman.util.DateUtils;
	
	import mx.utils.ObjectUtil;
	
	
	/**
	 *  The MTVDataTranslator is responsible marshaling MTV response data 
	 *  into typed objects.
	 */	
	public class MTVDataTranslator
	{
		//setup namespaces
		private namespace media = "http://search.yahoo.com/mrss/";
		private namespace openSearch = "http://a9.com/-/spec/opensearchrss/1.0/";
		private namespace atom = "http://www.w3.org/2005/Atom";
		
		public function MTVDataTranslator()
		{
		}
		
		
		public function parseSearchVideoResponse( result:* ):Array
		{
			var videos:Array = new Array();
		
			if ( result is XML || result is XMLList )
			{
				for each ( var entryXML:XML in result.atom::entry )
				{
					videos.push( parseVideoFromXML( entryXML ) );
				}
			}
			else
			{
				throw new Error( "parseSearchVideoResponse not supported for 'object' resultFormat.  Set to 'e4x' instead." );
			}
			
			return videos;
			
		}
		
		private function parseVideoFromXML( entryXML:XML ):VideoVO
		{
			var video:VideoVO = new VideoVO();
			video.id = entryXML.atom::id.toString();
			video.updated = DateUtils.parseW3CDTF( entryXML.atom::updated.toString() );
			try
			{
				//currently this field not sent correctly by API...supposed to be fixed soon 
				video.published = DateUtils.parseW3CDTF( entryXML.atom::published.toString() );
			}
			catch ( e:Error )
			{
				video.published = new Date(1970,0,1);
			}
			video.title = entryXML.atom::title.toString();
			video.content = entryXML.atom::content.toString();
			
			video.author.name =  entryXML.atom::author.atom::name.toString();
			video.author.uri =  entryXML.atom::author.atom::uri.toString();
			
			video.swfURL = entryXML.media::content.@url.toString();
			video.playerURL = entryXML.media::player.@url.toString();
			
			
			trace(entryXML.toXMLString());
			
			for each( var thumbXML:XML in entryXML.media::thumbnail )
			{
				video.thumbNails.push( parseThumbNailFromXML( thumbXML ));
			}
			
			return video;
		}
		
		private function parseThumbNailFromXML( thumbXML:XML ):ThumbNail
		{
			var thumbNail:ThumbNail = new ThumbNail();
			thumbNail.url = thumbXML.@url.toString();
			thumbNail.height = Number( thumbXML.@height );
			thumbNail.width = Number( thumbXML.@width );
			
			return thumbNail;
		}
		
		
		public function parseSearchArtistResponse( result:* ):Array
		{
			var response:Array = new Array();
		
			if ( result is XML || result is XMLList )
			{
				for each ( var entryXML:XML in result.atom::entry )
				{
					response.push( parseArtistFromXML( entryXML ) );
				}
			}
			else
			{
				throw new Error( "parseSearchArtistResponse not supported for 'object' resultFormat.  Set to 'e4x' instead." );
			}
			
			return response;
			
		}
		
		
		private function parseArtistFromXML( entryXML:XML ):Artist
		{
			var artist:Artist = new Artist();
			artist.id = entryXML.atom::id.toString();
			artist.updated = DateUtils.parseW3CDTF( entryXML.atom::updated.toString() );
			artist.title = entryXML.atom::title.toString();
			
			artist.author.name =  entryXML.atom::author.atom::name.toString();
			artist.author.uri =  entryXML.atom::author.atom::uri.toString();
			
			artist.videosURL = entryXML.atom::link[1].@href;
			artist.relatedArtistURL = entryXML.atom::link[2].@href;
			
			trace(entryXML.toXMLString());
			
			for each( var thumbXML:XML in entryXML.media::thumbnail )
			{
				artist.thumbNails.push( parseThumbNailFromXML( thumbXML ));
			}
			//trace( ObjectUtil.toString( artist ));
			return artist;
		}


		public function parseLyricsSearchResponse( result:* ):LyricsRequest
		{trace( result.toXMLString());
			var lr:LyricsRequest = new LyricsRequest();
			
			if ( result is XML || result is XMLList )
			{
				if( result.sg == 'Not found')
				{
					lr.album = 'Not found';
					lr.artist = 'Not found';
					lr.song = 'Not found';
					lr.lyricsHTML = 'Lyrics not found';
				}
				else
				{
					lr.album = result.sg.al.toString();
					lr.artist = result.sg.ar.toString();
					lr.song = result.sg.tt.toString();
					var brPattern:RegExp = /\[br\]/g;
					lr.lyricsHTML = result.sg.tx.toString();
					lr.lyricsHTML = lr.lyricsHTML.replace( brPattern, '<br>');
				}
			}
			else
			{
				throw new Error( "parseLyricsSearchResponse not supported for 'object' resultFormat.  Set to 'e4x' instead." );
			}
			
			return lr;
		}

		public function parseAmazonSearchResponse( result:* ):Array
		{
			var resultArray:Array = new Array();
		    //trace( ObjectUtil.toString( result )  )
			if ( !(result is XML || result is XMLList) )
			{
				//for( var i:int = 0; i < result.Items.length; i++ )
				for each( var obj:Object in result.Items.Item )
				{
					trace( ObjectUtil.toString( obj )  );
					resultArray.push( parseResultItemFromObject( obj ) );
				}
			}
			else
			{
				throw new Error( "parseAmazonSearchResponse not supported for XML resultFormat.  Set to 'object' instead." );
			}
			
			return resultArray;
		}
		
		private function parseResultItemFromObject( item:Object ):ResultItem
		{
			var resultItem:ResultItem = new ResultItem();
			
			resultItem.ASIN = item.ASIN.toString();
			resultItem.detailPageURL = item.DetailPageURL.toString();
			if( item.SmallImage )
			{
				resultItem.smallThumb.url = item.SmallImage.URL.toString();
				resultItem.smallThumb.height = Number(item.SmallImage.Height);
				resultItem.smallThumb.width = Number(item.SmallImage.Width);
			}
			if( item.LargeImage )
			{
				resultItem.largeThumb.url = item.LargeImage.URL.toString();
				resultItem.largeThumb.height = Number(item.LargeImage.Height);
				resultItem.largeThumb.width = Number(item.LargeImage.Width);
			}
			if( item.MediumImage )
			{
				resultItem.mediumThumb.url = item.MediumImage.URL.toString();
				resultItem.mediumThumb.height = Number(item.MediumImage.Height);
				resultItem.mediumThumb.width = Number(item.MediumImage.Width);
			}
			resultItem.author = item.ItemAttributes.Author.toString();
			if( item.ItemAttributes.Manufacturer )
				resultItem.manufacturer = item.ItemAttributes.Manufacturer.toString();
			resultItem.title = item.ItemAttributes.Title.toString();
			resultItem.productGroup = item.ItemAttributes.ProductGroup.toString();
			resultItem.releaseDate = new Date(item.ItemAttributes.OriginalReleaseDate);
			
			for each( var obj:Object in item.ItemLinks )
			{
				switch( obj.Description )
				{
					case 'Add to Wish List':
						resultItem.addToWishListURL = obj.URL;
						break;
					
				}
			}
			
			return resultItem;
		}

	}
}